//
//  WWSupermarketViewController.m
//  Shopping
//
//  Created by 张湾湾 on 16/8/18.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWSupermarketViewController.h"
#import "WWLeftTableViewCell.h"
#import "WWRightTableViewCell.h"
#import "WWLeftCellModel.h"
#import "WWRightCellModel.h"
#import "WWGoodsDetailViewController.h"
#import "SVProgressHUD.h"



@interface WWSupermarketViewController () <UITableViewDataSource,UITableViewDelegate,WWRightTableViewCellDelegate>

/**左边产品分类列表*/
@property (nonatomic,weak) UITableView *leftTableView;
/**右边产品详情列表*/
@property (nonatomic,weak) UITableView *rightTableView;
/**左边列表数组模型,产品分类*/
@property (nonatomic,strong) NSArray<WWLeftCellModel *> *leftDataList;


@end

static NSString *leftIdentifierID = @"leftCell";
static NSString *rightIdentifierID = @"rightCell";

@implementation WWSupermarketViewController

#pragma mark
#pragma mark - ***************刷新数据
- (void)setLeftDataList:(NSArray<WWLeftCellModel *> *)leftDataList {
    _leftDataList = leftDataList;
    [self.leftTableView reloadData];
    [self.rightTableView reloadData];
    
    [SVProgressHUD dismiss];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //------ 保存回调回来的数据,同时刷新列表,左边列表
    [WWLeftCellModel leftModelWithDataSuccess:^(NSArray<WWLeftCellModel *> *array) {
        self.leftDataList = array;
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    [self setupUI];
}

#pragma mark
#pragma mark - ***************初始化界面
- (void)setupUI {
    
    CGFloat maxY = CGRectGetMaxY(self.navigationController.navigationBar.frame);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UITableView *leftTableView = [[UITableView alloc] init];
    self.leftTableView = leftTableView;
    [self.view addSubview:leftTableView];
    
    UITableView *rightTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.rightTableView = rightTableView;
    [self.view addSubview:rightTableView];
    
    [leftTableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(maxY);
        make.leading.equalTo(0);
        make.size.equalTo(CGSizeMake(SCREENW * 0.3, SCREENH - 113));
    }];
    [rightTableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(maxY);
        make.leading.equalTo(leftTableView.trailing);
        make.size.equalTo(CGSizeMake(SCREENW * 0.7, SCREENH -  113));
    }];
    
    leftTableView.dataSource = self;
    leftTableView.delegate = self;
    rightTableView.dataSource = self;
    rightTableView.delegate = self;
    
    leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [leftTableView registerClass:[WWLeftTableViewCell class] forCellReuseIdentifier:leftIdentifierID];
    [rightTableView registerClass:[WWRightTableViewCell class] forCellReuseIdentifier:rightIdentifierID];
    
/**添加到navigationBar下面,导航栏和TabBar可以点击*/
//    UIView *iv = [[UIView alloc] initWithFrame:self.view.frame];
//    iv.backgroundColor = [UIColor redColor];
//    [self.navigationController.topViewController.view addSubview:iv];
//
//    [iv insertSubview:[SVProgressHUD new] belowSubview:self.navigationController.navigationBar];
    
    
    
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showWithStatus:@"拼命加载中..."];
 

}


#pragma mark
#pragma mark - ***************<UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if([tableView isEqual:self.leftTableView]){
        return 1;
    } else {
        return self.leftDataList.count;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if([tableView isEqual:self.leftTableView]){
        return self.leftDataList.count;
    } else {
        WWLeftCellModel *model = self.leftDataList[section];
        NSArray<WWRightCellModel *> *tempArr = model.rightArray;
        return tempArr.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if([tableView isEqual:self.leftTableView]){
        
        WWLeftTableViewCell *leftCell = [tableView dequeueReusableCellWithIdentifier:@"leftCell" forIndexPath:indexPath];
        WWLeftCellModel *model = self.leftDataList[indexPath.row];
        leftCell.model = model;
        return leftCell;
    }else {
        
        WWRightTableViewCell *rightCell = [tableView dequeueReusableCellWithIdentifier:@"rightCell" forIndexPath:indexPath];
        rightCell.delegate = self;
        
        rightCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        WWLeftCellModel *model = self.leftDataList[indexPath.section];
        NSArray<WWRightCellModel *> *tempArr = model.rightArray;
        rightCell.model = tempArr[indexPath.row];
        return rightCell;
    }
    return nil;
    
}


#pragma mark
#pragma mark - ***************<UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if([tableView isEqual:self.leftTableView]){
        
        NSIndexPath *indexPaths = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
        [_rightTableView scrollToRowAtIndexPath:indexPaths atScrollPosition:UITableViewScrollPositionTop animated:YES];

    } else if ([tableView isEqual:self.rightTableView]){

        NSIndexPath *indexPaths = [NSIndexPath indexPathForRow:indexPath.section inSection:0];
        [_leftTableView selectRowAtIndexPath:indexPaths animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        
        WWLeftCellModel *model = self.leftDataList[indexPath.section];
        NSArray<WWRightCellModel *> *tempArr = model.rightArray;
        WWRightCellModel *rightModel = tempArr[indexPath.row];
        
        WWGoodsDetailViewController *goodsDetailVC = [[WWGoodsDetailViewController alloc] init];
        goodsDetailVC.navigationItem.title = rightModel.name;
        [self.navigationController pushViewController:goodsDetailVC animated:YES];
        
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if([tableView isEqual:self.rightTableView]){
        WWLeftCellModel *model = self.leftDataList[section];
        return model.name;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if([tableView isEqual:self.rightTableView]){
        
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 25)];
        headerView.backgroundColor = [UIColor colorWithRed:(255)/256.0 green:(200)/256.0 blue:(187)/256.0 alpha:1.0];
        
        WWLeftCellModel *model = self.leftDataList[section];
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, SCREENW - 20, 20)];
        [headerView addSubview:headerLabel];
        headerLabel.textColor = [UIColor darkGrayColor];
        headerLabel.text = model.name;
        return headerView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if([tableView isEqual:self.rightTableView]){
        return 80;
    }
    return 40;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
    if (scrollView == self.leftTableView) return;

    //------ 获取右列表可见行
    NSIndexPath *rightTableTopHeaderViewIndexpath = [[self.rightTableView indexPathsForVisibleRows] firstObject];
    
//    NSLog(@"right = %@",rightTableTopHeaderViewIndexpath);
  
    //------ 左列表联动
    NSIndexPath *leftTableSelectIndexpath = [NSIndexPath indexPathForRow:rightTableTopHeaderViewIndexpath.section inSection:0];
    [self.leftTableView selectRowAtIndexPath:leftTableSelectIndexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    

    /**笨方法,可以实现*/
//    NSArray *offsetArr = @[@(0),@(1268),@(2376),@(2884),@(5152),@(14700),@(15848),@(33316),@(41344),@(41852),@(42920),@(46148),@(48496),@(51244),@(52398)];
//    
//    for (int i = 0; i < offsetArr.count; i++) {
//        
//        if (i <= 13) {
//            if (self.rightTableView.contentOffset.y >= [offsetArr[i] floatValue] && self.rightTableView.contentOffset.y < [offsetArr[i + 1] floatValue]) {
//                
//                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
//                [_leftTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//            }
//        } else if (self.rightTableView.contentOffset.y >= [offsetArr[13] floatValue] && self.rightTableView.contentOffset.y < [offsetArr[14] floatValue]) {
//            
//            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:13 inSection:0];
//            [_leftTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//        }
//    }
//    
    

//    NSLog(@"%lf",self.rightTableView.contentOffset.y);
}



#pragma mark
#pragma mark - ***************<WWRightTableViewCellDelegate>
- (void)increaseShoppingCar:(WWRightTableViewCell *)rightTableCell {
    NSLog(@"%s",__func__);
    
}

- (void)reduceShoppingCar:(WWRightTableViewCell *)rightTableCell {
    NSLog(@"%s",__func__);
}

#pragma mark
#pragma mark - ***************懒加载




@end
